<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


class RetrospectivesController extends AppController{
    public $helpers= array('Html','Form','Js');
    public $component=array('RequestHandler');
    
    //AUTORIZACION
    public function isAuthorized($user) {
        // All registered users can add posts
        if (in_array($this->action, array('add','validate_form'))) {
            return true;
        }

        // The owner of a post can edit and delete it
        if (in_array($this->action, array( 'delete'))) {
            $postId = $this->request->params['pass'][0];
            if ($this->Post->isOwnedBy($postId, $user['id'])) {
                return true;
            }
        }

        return parent::isAuthorized($user);
    }
    
    
    public function validate_form(){
        if($this->request->is('ajax')){
            $field = $this->request->data['field'];
            $value = $this->request->data['value'];
            $this->request->data['Retrospective'][$field] = $value;
            $this->Retrospective->set($this->data);
            if($this->Retrospective->validates()){
                $this->autoRender=FALSE;
            }
            else{
                $error=$this->validateErrors($this->Retrospective);
                $this->set('error',$error[$field]);
                $this->layout='ajax';
            }
        }
    }
    
    public function add(){
        if ($this->request->is('post')){  //ZP: se verifica en con CakeRequest::is() si es un HTTP POST request
            $this->request->data['Retrospective']['creator_id'] = $this->Auth->user('id'); //Added this line
//            $project=$this->request->data['Retrospective']['project_id'];
            $ret=$this->request->data;
            if($this->Retrospective->save($ret)){
//                if($this->request->is('ajax')){
                    $this->layout='ajax';
                    $this->render('success','ajax');
//                }
//                else{
//                    $this->Session->setFlash('Your retrospective has been saved.'.$ret['Retrospective']['id']);
//                    $this->redirect(array('controller'=>'projects','action'=>'retrospective',$project));
//                }
            }
            else{
//                if($this->request->is('ajax')){
                    $this->layout='ajax';
                    $this->render('error','ajax');
//                }
//                else{
//                    $this->Session-> setFlash('Unable to add your post');
//                }
            }
        }
    }
    
    public function delete($id) {
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Retrospective->delete($id)) {
            $this->Session->setFlash('The retrospective with id: ' . $id . ' has been deleted.');
            $this->redirect(array('action' => 'add'));
        }
    }
    
    
    
    
}
?>
